package zen2d;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class SwingWindow implements Window {

    private int width;
    private int height;

    private JFrame targetFrame;
    private BufferedImage image;
    private GraphicsContext graphicsContext;
    private static boolean highQuality = true; //高质量缩放，默认开启

    private static int viewportX = 0;
    private static int viewportY = 0;
    private static int drawX = 0;
    private static int drawY = 0;

    private KeyEventListener keyEventListener;
    private MouseEventListener mouseEventListener;

    @Override
    public void setKeyEventListener(KeyEventListener keyEventListener) {
        this.keyEventListener = keyEventListener;
    }

    @Override
    public void setMouseEventListener(MouseEventListener mouseEventListener) {
        this.mouseEventListener = mouseEventListener;
    }

    @Override
    public void moveViewport(int offsetX, int offsetY) {
        viewportX += offsetX;
        viewportY += offsetY;

        drawX = -viewportX;
        drawY = -viewportY;
    }

    @Override
    public int getViewportX() {
        return viewportX;
    }

    @Override
    public int getViewportY() {
        return viewportY;
    }

    @Override
    public boolean isHighQuality() {
        return highQuality;
    }

    @Override
    public void setHighQuality(boolean highQuality) {
        SwingWindow.highQuality = highQuality;
    }

    static {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    SwingWindow() {
    }


    @Override
    public void clearRect(int x, int y, int width, int height) {
        graphicsContext.clearRect(x, y, width, height);
    }

    @Override
    public void repaint() {
        targetFrame.repaint();
    }

    @Override
    public void createWindow(int x, int y, int width, int height) {

        this.width = width;
        this.height = height;

        image = new BufferedImage(10000, 10000, BufferedImage.TYPE_INT_ARGB);
        graphicsContext = new GraphicsContext(image);

        targetFrame = new JFrame("zen2d window");
        targetFrame.setBounds(x, y, width, height);
        targetFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        targetFrame.setContentPane(new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.drawImage(image, drawX, drawY, null);
            }
        });
        targetFrame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                keyEventListener.keyDown(e.getKeyCode());
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                keyEventListener.keyUp(e.getKeyCode());
            }
        });
        targetFrame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                mouseEventListener.mousePressed(new Point(e.getX(), e.getY()));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                mouseEventListener.mouseReleased(new Point(e.getX(), e.getY()));
            }

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                super.mouseWheelMoved(e);
                mouseEventListener.mouseWheelMoved(e.getWheelRotation());
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                mouseEventListener.mouseDragged(new Point(e.getX(), e.getY()));
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                mouseEventListener.mouseMoved(new Point(e.getX(), e.getY()));
            }
        });

//        resetWindow();

        targetFrame.setResizable(false);
        targetFrame.setVisible(true);

        System.err.println(width + " * " + height);
    }

    @Override
    public void resetWindow() {
        Insets inset = targetFrame.getInsets();
        int realWidth = width + inset.left + inset.right;
        int realheight = height + inset.top + inset.bottom;
        targetFrame.setSize(realWidth, realheight);
    }

    @Override
    public void setTitle(String title) {
        targetFrame.setTitle(title);
    }

    @Override
    public void showMsg(String msg) {
        JOptionPane.showMessageDialog(targetFrame, msg, "消息", JOptionPane.PLAIN_MESSAGE);
    }

    @Override
    public GraphicsContext getGraphicsContext() {
        return graphicsContext;
    }


}
